<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/12 0012
 * Time: 上午 7:41
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class UserLesson
{
    private $tableName = 'user_lesson';
    private $tableDB = null;
    private $id;
    private $error = 0;
    private $maxCount = 10;
    private $message = [
        0 => '无数据',
        1 => '写入数据库出错',
        2 => '更新数据库失败',
        3 => '将要删除的数据不存在'
    ];

    public function __construct( $id = null , $userId = null)
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $id !== null )
        {
            $this->id = $id;
            $this->tableDB = $this->tableDB->where( $this->tableName . '.id', '=', $id);
        }

        if ( $userId !== null )
        {
            $this->tableDB = $this->tableDB->where( $this->tableName . '.user_id', '=', $userId);
        }
    }

    public function getThisLesson()
    {
        $result =  $this->tableDB;

        $result = $result -> join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_lesson.*',
                'books.book',
                'lessons.number as lesson_number'
            );

        $result = $result -> get();

        if ( $result )
        {
            return $result[0];
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getUserLessons( $userId )
    {
        $result = $this->tableDB->where('user_id', '=', $userId);

        $result = $result -> join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_lesson.*',
                'books.book',
                'lessons.number as lesson_number'
            );

        $result = $result -> get();
        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function addLesson( array $data )
    {
        $result = $this->tableDB->insert($data);
        //return $result ? true : false;
        if ( $result )
        {
            return true;
        }
        else
        {
            $this->error = 1;
            return false;
        }
    }

    public function updateLesson( array $data = null )
    {
        $result = $this->tableDB->update($data);

        if ($result)
        {
            return true;
        }
        else
        {
            $this->error = 2;
            return false;
        }
    }

    public function delLesson( $userId )
    {
        $result = $this->tableDB->where('user_id','=', $userId)->delete();

        if ( $result >= 1 )
        {
            return true;
        }
        else
        {
            $this->error = 3;
            return false;
        }
    }

    public function userLessonCount( $userId )
    {
        $result = $this->tableDB->where('user_id', '=', $userId)->count();

        return $result;
    }

    public function getMaxCount()
    {
        return $this->maxCount;
    }

    public function getTestWordsWithOutRemember($userId)
    {
        $result = $this->tableDB;

        $result = $result
            ->join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            ->join('user_remember', 'user_remember.user_id', '=', 'user_lesson.user_id')
            ->join('words', 'words.lesson_id', '=', 'lessons.id')

            ->where('user_remember.user_id', '=', $userId)
            ->where('user_lesson.lesson_id', '=', $this->id)
//            ->where('words.id', '<>', 'user_remember.word_id')
            ->get();
        return $result;
    }

    public function getLessonByDate($userId, $date)
    {
        $result = $this->tableDB;

        $result = $result->where('user_id', '=', $userId)
            ->where('date', '=', $date);

        $result = $result
            -> join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_lesson.*',
                'books.book',
                'lessons.number as lesson_number');

        $result = $result->get();

        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getLessonByWeek($userId,$date)
    {
        $result = $this->tableDB;

        $date6 = strtotime('+6 day', strtotime($date));

        $date6 = date('Y-m-d', $date6);

//        dd($date.'       '.$date6);

        $result = $result->where('user_id', '=', $userId)
            ->where('date', '>=', $date)
            ->where('date', '<=', $date6);

        $result = $result
            -> join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_lesson.*',
                'books.book',
                'lessons.number as lesson_number');

        $result = $result->get();

        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getLessonByMonth($date)
    {
        $result = $this->tableDB;

        $result = $result->where('date', 'like', $date . '%');

        $result = $result
            -> join('lessons', 'lessons.id', '=', 'user_lesson.lesson_id')
            -> join('books', 'books.id', '=', 'lessons.book_id') -> select(
                'user_lesson.*',
                'books.book',
                'lessons.number as lesson_number');

        $result = $result->get();

        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getLessonId()
    {
        $result = $this->tableDB;

        $result = $result->select('lesson_id')->get();

        if ( $result )
        {
            return $result[0]->lesson_id;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}